package com.example.xmpic.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.common.base.OnCallBackToEdit;
import com.example.xmpic.R;
import com.example.xmpic.R2;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2020-03-21.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：含输入的弹出框
 */
public class InputPwdDialog extends Dialog {

    @BindView(R2.id.et_input)
    EditText etInput;
    @BindView(R2.id.iv_close)
    ImageView ivClose;
    @BindView(R2.id.tv_title)
    TextView tvTitle;
    @BindView(R2.id.tv_dialog_confirm)
    Button tvDialogConfirm;

    private Context mContext;
    private String mTitle;
    private String mConfirm;
    private OnCallBackToEdit mCallBack;

    public InputPwdDialog(Context context, OnCallBackToEdit callBack) {
        super(context, R.style.window_background2);
        this.mCallBack = callBack;
        this.mContext = context;
    }

    public void setTitle(String title) {
        this.mTitle = title;
    }


    public void setTitleAndButton(String title, String confirm) {
        this.mTitle = title;
        this.mConfirm = confirm;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_edit);
        setCanceledOnTouchOutside(false);
        ButterKnife.bind(this);
        tvDialogConfirm.setEnabled(false);
        tvDialogConfirm.getBackground().setAlpha(72);
        tvTitle.setText(mTitle);
        if (mConfirm!=null) tvDialogConfirm.setText(mConfirm);

        tvDialogConfirm.setOnClickListener(v -> {
            mCallBack.onConfirm(etInput.getText().toString());
            dismiss();
        });

        ivClose.setOnClickListener(v -> {
            mCallBack.onCancel();
            dismiss();
        });

        etInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length()>0){
                    tvDialogConfirm.setEnabled(true);
                }else {
                    tvDialogConfirm.setEnabled(false);
                }
            }
        });
    }
}
